package com.hn658.user.core.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.user.common.entity.EmployeeEO;
import com.hn658.user.core.dao.db.IEmployeeDBDAO;
import com.hn658.user.core.service.IEmployeeService;
import com.hn658.user.sync.user.Employee;
import com.hn658.user.sync.user.EmployeeHelper;
@Transactional(propagation=Propagation.SUPPORTS)
@Service
public class EmployeeServiceImpl  extends AbstractBusinessObject implements IEmployeeService{

	@Autowired
	private IEmployeeDBDAO employeeDBDAO;
	
	@Override
	public GenericPage<EmployeeEO> queryEmployee(Map<String, Object> queryMap, int limit, int start, String sortBy,
			boolean isAsc) {
		if(StringUtils.isEmpty(sortBy)){
			sortBy = "uid";
		}
		GenericPage<EmployeeEO> employeeList = employeeDBDAO.selectByMap(queryMap, limit, start, sortBy, isAsc);
		return employeeList;
	}

	public EmployeeEO getEmployeeEOById(Long id){
		return employeeDBDAO.selectById(id);
	}

	@Override
	public void addEmployee(EmployeeEO employeeEO) {
		if(employeeEO==null){
			throw new BusinessException("不能为空");
		}
		Employee employee=new Employee();
		employee.setEmail(employeeEO.getEmail());
		employee.setJobnumber(employeeEO.getJobnumber());
		employee.setMobile(employeeEO.getMobile());
		employee.setName(employeeEO.getEmpName());
		employee.setPosition(employeeEO.getEmpPosition());
		List<Long> list=new ArrayList<Long>();
		list.add(employeeEO.getDeptId());
		employee.setDepartment(list);
		String createUser = EmployeeHelper.createUser(employee);
		employeeEO.setUid(createUser);
		employeeDBDAO.addEmployee(employeeEO);
		
	}

	@Override
	public void deleteEmployee(String id) {
		if(id==null){
			throw new BusinessException("id不能为空");
		}
		EmployeeHelper.deleteUser(id);
		employeeDBDAO.deleteEmployee(id);
		
	}

	@Override
	public void updateEmployee(EmployeeEO employeeEO) {
		if(employeeEO==null){
			throw new BusinessException("不能为空");
		}
		Employee employee=new Employee();
		employee.setUserid(employeeEO.getUid());
		employee.setEmail(employeeEO.getEmail());
		employee.setJobnumber(employeeEO.getJobnumber());
		employee.setMobile(employeeEO.getMobile());
		employee.setName(employeeEO.getEmpName());
		employee.setPosition(employeeEO.getEmpPosition());
		List<Long> department=new ArrayList<Long>();
		department.add(employeeEO.getDeptId());
		employee.setDepartment(department);
		EmployeeHelper.updateUser(employee);
		employeeDBDAO.updateEmployee(employeeEO);
		
	}
}
